package com.starry.core.common.token.context;

import com.alibaba.ttl.TransmittableThreadLocal;

/**
 * token
 *
 * @author xiaoke
 */
public class TokenContextHolder {

    /**
     * 当前token
     */
    private static final ThreadLocal<String> TOKEN = new TransmittableThreadLocal<>();

    public static String getToken() {
        return TOKEN.get();
    }

    public static void setToken(String token) {
        TOKEN.set(token);
    }

    public static String getRequiredToken() {
        String token = getToken();
        if (token == null) {
            throw new NullPointerException("当前线程中未获取到token");
        }
        return token;
    }

    public static void clear() {
        TOKEN.remove();
    }
}
